package cn.luckdou.system.controller.commons;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.luckdou.commons.pojo.FilePojo;
import cn.luckdou.commons.pojo.FileResult;
import cn.luckdou.commons.utils.JsonUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.luckdou.commons.pojo.PictureResult;
import cn.luckdou.commons.utils.AliyunOSSClientUtil;

@Controller
@RequestMapping("/pic")
public class PictureController {
	/*// 阿里云API的bucket名称
	public static String BACKET_NAME = "dd";
	public static String url = "http://oss.dd.com";
	// 阿里云API的文件夹名称
	public static String FOLDER = "img";
	public static OSSClient ossClient = null;
	static {
		// 初始化OSSClient
		ossClient = AliyunOSSClientUtil.getOSSClient();
	}*/

	@RequestMapping("/upload")
	@ResponseBody
	public static String uploadFile(HttpServletRequest request, HttpServletResponse response) {
		PictureResult result = new PictureResult();
		// 上传到图片服务器
		try {
			FilePojo uploads = AliyunOSSClientUtil.uploads(request);
			// 把url响应给客户端
			result.setError(0);
			result.setUrl(uploads.getUrl());
		} catch (Exception e) {
			e.printStackTrace();
			result.setError(1);
			result.setMessage("图片上传失败");
		}
		// 需要把java对象手工转换成json数据。
		String json = JsonUtils.toJson(result);
		System.out.println("上传路径:" + json);
		return json;
	}

	@RequestMapping("/fileUpload")
	@ResponseBody
	public static String fileUpload(HttpServletRequest request) {
		FileResult result = new FileResult();
		// 上传到图片服务器
		try {
			FilePojo filePojo = AliyunOSSClientUtil.uploads(request);
			// 把url响应给客户端
			result.setError(0);
			result.setUrl(filePojo.getUrl());
			result.setName(filePojo.getName());
		} catch (Exception e) {
			e.printStackTrace();
			result.setError(1);
			result.setMessage("文件上传失败");
		}
		// 需要把java对象手工转换成json数据。
		String json = JsonUtils.toJson(result);
		System.out.println("上传路径:" + json);
		return json;
	}

	/*public static String upload(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 创建一个通用的多部分解析器
		CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
		String imgUrl = "";
		// 判断 request 是否有文件上传,即多部分请求
		if (multipartResolver.isMultipart(request)) {
			// 转换成多部分request
			MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
			// 取得request中的所有文件名
			Iterator<String> iter = multiRequest.getFileNames();
			while (iter.hasNext()) {
				// 取得上传文件
				MultipartFile file = multiRequest.getFile(iter.next());
				if (file != null) {
					// 取得当前上传文件的文件名称
					String fileName = file.getOriginalFilename();
					// 取扩展名不要“.”
					String extName = fileName.substring(fileName.lastIndexOf(".") + 1);
					String new_img_name = "";
					String filePath = "";
					if (file.getSize() > 1024) {
						new_img_name = IDUtils.genImageName() + "." + extName;
						filePath = request.getSession().getServletContext().getRealPath("filetemp") + "//" + new_img_name;
						filePath = filePath.trim();
						System.out.println("写入:" + filePath);
						Thumbnails.of(file.getInputStream()).scale(0.1f).outputFormat("jpg").toFile(new File(filePath));
					}
					
					File file2 = new File(filePath);
					String md5key = AliyunOSSClientUtil.uploadObject2OSS(ossClient, file2, BACKET_NAME, FOLDER);
					System.out.println("md5签名:" + md5key);
					if (file2.exists()) {
						file2.delete();
					}
					imgUrl=url+"/"+FOLDER+"/"+new_img_name+",";
				}
			}
			if (imgUrl.lastIndexOf(",") != -1) {
				imgUrl = imgUrl.substring(0, imgUrl.length() - 1);
			}
		}
		System.out.println("上传路径:" + imgUrl);
		return StringUtils.isNotBlank(imgUrl) ? imgUrl : "";

	}
	

	public static byte[] getContent(File file) throws IOException {
		byte[] buffer = null;
		try {
			long fileSize = file.length();
			if (fileSize > Integer.MAX_VALUE) {
				System.out.println("file too big...");
				return null;
			}
			FileInputStream fi = new FileInputStream(file);
			buffer = new byte[(int) fileSize];
			int offset = 0;
			int numRead = 0;
			while (offset < buffer.length && (numRead = fi.read(buffer, offset, buffer.length - offset)) >= 0) {
				offset += numRead;
			}
			// 确保所有数据均被读取
			if (offset != buffer.length) {
				throw new IOException("Could not completely read file " + file.getName());
			}
			fi.close();
			return buffer;
		} catch (Exception e) {
			return buffer;
		}
	}*/
}
